<script setup lang="ts">
import { getCurrentInstance, onMounted, onUnmounted, ref } from 'vue'
import { switchTab } from '@uni-helper/uni-promises'
import { get } from 'lodash-es'
import { getAdvanceWechat, getSalaryAli, getSalaryWechat } from '@/package-mine/api/pay'

const status = ref(0)
const message = ref('')
const eventChannel = ref()

const salaryData = ref()

const platform = ref('alipay')
const from = ref('')
const transferScene = ref('')
/***
 * 返回首页
 */
function onToHome() {
    switchTab({
        url: '/pages/mine/mine',
    })
}

/**
 * 获取支付宝薪资状态
 *
 * @returns Promise<void> 返回无具体结果的 Promise 对象
 */
async function getSalaryStatus() {
    const res = await getSalaryAli({
        id: salaryData.value.id,
        phone: salaryData.value.phone,
        detailOrderNo: salaryData.value.detailOrderNo,
    }).catch((err) => {
        status.value = 3
        message.value = err?.msg || '提现失败'
        throw new Error(err?.msg || '提现失败')
    })
    if (res?.msg === '转账处理中，请稍后') {
        status.value = 1
        await wait(3000)
        getSalaryStatus()
    }
    else {
        status.value = 2
        message.value = res?.msg || '提现成功'
    }
}

/**
 * 获取微信薪资状态
 *
 * @returns Promise<void> 返回无具体结果的 Promise 对象
 */
async function getWechatSalaryStatus(from: string = '') {
    if (from === 'advance') {
        const res = await getAdvanceWechat({
            id: salaryData.value.payid,
            detailOrderNo: salaryData.value.order,
            transferScene: transferScene.value,
        }).catch((err) => {
            status.value = 3
            message.value = err?.msg || '提现失败'
            throw new Error(err?.msg || '提现失败')
        })
        if (res.code === 500 || res?.type === '转账失败') {
            status.value = 1
            await wait(3000)
            getWechatSalaryStatus('addvance')
        }
        else {
            status.value = 2
            message.value = res?.msg || '提现成功'
        }
    }
    else {
        const res = await getSalaryWechat({
            id: salaryData.value.payid,
            detailOrderNo: salaryData.value.order,
            transferScene: transferScene.value,
        }).catch((err) => {
            status.value = 3
            message.value = err?.msg || '提现失败'
            throw new Error(err?.msg || '提现失败')
        })
        if (res.code === 500 || res?.type === '转账失败') {
            status.value = 1
            await wait(3000)
            getWechatSalaryStatus()
        }
        else {
            status.value = 2
            message.value = res?.msg || '提现成功'
        }
    }
}

onMounted(async () => {
    const instance = getCurrentInstance()
    eventChannel.value = get(instance, 'proxy.getOpenerEventChannel', new Function()).call(instance?.proxy)
    eventChannel.value.on('responseData', (data: AnyObject) => {
        status.value = data?.status || 0
        message.value = data?.message || ''
        salaryData.value = data
        platform.value = data?.platform || 'alipay'
        from.value = data.from || ''
        transferScene.value = data.transferScene || ''
        console.log(12345, salaryData.value)
    })

    if (salaryData.value?.id) {
        await wait(3000)
        if (platform.value === 'wechat')
            getWechatSalaryStatus(from.value === 'advance' ? 'advance' : '')
        else if (platform.value === 'alipay')
            getSalaryStatus()
    }
})

onUnmounted(() => {
    eventChannel.value.emit('refresh', { data: true })
})
</script>

<template>
    <view class="h-full">
        <view class="box-border p-32rpx">
            <view class="box-border w-full flex flex-col items-center pt-60rpx">
                <view class="mb-24rpx">
                    <img class="status-icon"
                         :src="`../../../static/images/tips/${status === 1 ? 'chulizhong' : status === 2 ? 'success' : 'error'}.png`"
                         mode="widthFix">
                </view>
                <view class="mb-40rpx text-center text-36rpx text-#ffffff font-600">
                    <view>{{ status === 1 ? '处理中' : status === 2 ? '提现成功' : '提现失败' }}</view>
                    <view v-if="status === 2 && platform === 'alipay'">薪资已发放至支付宝账户</view>
                    <view v-if="status === 2 && platform === 'wechat'">
                        <view class="mt-30rpx text-28rpx">薪资已发放至「微信-子弹工作」小程序<text class="text-orange">指定账户</text></view>
                    </view>
                </view>
                <view class="mb-30rpx text-28rpx text-#ffffff">
                    <view>{{ message || '提现处理中，请耐心等待哦！' }}</view>
                </view>

                <view class="w-full flex items-center justify-center gap-30rpx">
                    <view
                        class="box-border h-82rpx min-w-190rpx flex items-center justify-center border-2rpx border-#ffffff rounded-40rpx border-solid px-30rpx text-32rpx text-#ffffff"
                        @click="onToHome">
                        返回首页
                    </view>
                    <!-- <view v-if="status === 1"
                          class="box-border h-82rpx min-w-190rpx flex items-center justify-center gap-20rpx rounded-40rpx bg-#E66918 px-30rpx text-32rpx text-#ffffff">
                        <img class="h-36rpx w-36rpx" src="../../../static/images/telephone/phone.png" mode="widthFix">
                        联系领队
                    </view> -->
                </view>
            </view>
        </view>
    </view>
</template>

<style scoped>
.status-icon {
    width: 186rpx;
    height: 186rpx;
}

.operation-btns {
    justify-content: center;
    margin-top: 30rpx;
}
</style>

<route lang="json">
{
    "layout": "transparent",
    "style": {
        "navigationBarTextStyle": "white",
        "navigationBarTitleText": "",
        "mp-alipay": {
            "transparentTitle": "always"
        }
    }
}
</route>
